﻿using System;
using System.Collections.Generic;
using System.Text;
using Spring.Context.Support;
using Spring.Context;

namespace Messenia.GUI.Console.ServiceResolver
{
    class IntefaceResolver
    {
        private static IntefaceResolver appContext;
        private IApplicationContext springContext = null;
        private IntefaceResolver()
        {
            try
            {
                springContext = ContextRegistry.GetContext();
            }
            catch (Exception ex)
            {
                throw new Exception("Can't get the context of the Application", ex);
            }
        }
        /// <summary>
        /// Provide a Unique instance of AppContext
        /// </summary>
        private static IntefaceResolver Instance
        {
            get
            {
                if (appContext == null)
                {
                    appContext = new IntefaceResolver();
                }

                return appContext;
            }
        }
        /// <summary>
        /// Return a instance of object in the context by a given name
        /// </summary>
        /// <param name="objectName">Name of the solicited object</param>
        /// <returns>A instance of the object, in other case an exception</returns>
        public static T Resolve<T>(string objectName)
        {
            return (T)Instance.springContext.GetObject(objectName);
        }

        /// <summary>
        /// Return a instance that correspond with the interface
        /// </summary>
        /// <returns></returns>
        public static T Resolve<T>()
        {
            return (T)Instance.springContext.GetObject(typeof(T).Name);
        }
    }
}
